<?php
/**
 * Created by PhpStorm.
 * User: geolo
 * Date: 2018/8/13
 * Time: 14:58
 */
namespace app\modules\mch\models;
use app\models\LiveIndex;
class LiveIndexForm extends Model
{

    public $wechat_app_id; // 小程序id
    public $status;
    public $actor_imgurl;
    public $number;
    public $total;
    public $room_name;
    public $name;
    public $begin_time;
    public $end_time;
    public function rules()
    {
        return [
            [['wechat_app_id'], 'trim'],
            [['wechat_app_id'], 'required'],
        ];
    }

    public function attributeLabels()
    {
        return [
            'wechat_app_id' => '请先申请小程序',

        ];
    }

    public function save()
    {
        if (!$this->validate())
            return $this->getModelError();
        $model = new LiveIndex();
        $model->attributes = $this->attributes;
        $model->wechat_app_id = $this->wechat_app_id;
        $model->room_name =$this->room_name;
        $model->name = $this->name;
        $count = LiveIndex::find()->where(['wechat_app_id'=>$this->wechat_app_id,'is_delete'=>0])->count();
//        var_dump($count);
        if($count<=0) {
            $model->is_delete=0;
            if ($model->save()) {
                return [
                    'code' => 0,
                    'msg' => '保存成功',
                ];
            }
        }else{
            $upd = LiveIndex::findOne(['wechat_app_id'=>$this->wechat_app_id]);
            $upd->status = $this->status;
            $upd->actor_imgurl = $this->actor_imgurl;
            $upd->number = $this->number;
            $upd->total = $this->total;
            $upd->room_name =$this->room_name;
            $upd->name = $this->name;
            $upd->begin_time = $this->begin_time;
            $upd->end_time = $this->end_time;
            if($upd->save()){
                return [
                    'code' => 0,
                    'msg' => '保存成功',
                ];
            }
        }
        return $this->getModelError($model);
    }

}
